const btnLogin = document.querySelector('.btn-login')//登录按钮
const loginForm = document.querySelector('.login-form')//登录按钮


// 登录按钮
loginForm.addEventListener('submit', function (e) {
	// 取消默认行为
	e.preventDefault()
	const data =serialize(loginForm,{hash:true,empty:true})
	if (!/[a-zA-Z0-9]{2,30}/.test(data.username)) {
		toast('请输入账号2-30')
		return
	}
	if (!/[a-zA-Z0-9]{6,30}/.test(data.password)) {
		toast('请输入密码6-30')
		return
	}

	axios({ url: '/login', method: 'post', data })
	.then(res => {
		toast(res.message)
		console.log(res);
		localStorage.setItem('token',res.data.token)
		localStorage.setItem('username',res.data.username)

		setTimeout(() => {
			location.href='./index.html'
		}, 1000);
	}).catch(err => {
		console.dir(err);
		toast(err.response.data.message)
	})
})